<template>
  <el-dialog title="关键词配置" v-model="visible" width="1000px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="关键字" prop="keyWord">
          <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入关键字名字"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="关键字名字" align="center" prop="keyWord" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button :type="scope.row.selected ? 'info' : 'primary'" @click="toggle(scope.row)" size="small">{{scope.row.selected ? "取消选择" : "选择"}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <div class="btn-group" style="text-align:center;margin-top:24px;">
      <el-button @click="close">取消</el-button>
      <el-button @click="submit" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { KeyWordApi, KeyWordVO } from '@/api/lm/keyword'

/** 关键字 列表 */
defineOptions({ name: 'KeywordChooserRef' })
const visible = ref(false);
const loading = ref(true) // 列表的加载中
const list = ref<KeyWordVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const chooseList = ref<any[]>([]);
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  keyWord: undefined
})
const queryFormRef = ref() // 搜索的表单
const emit = defineEmits(["submit"]);

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await KeyWordApi.getKeyWordPage(queryParams)
    list.value = data.list
    total.value = data.total

    list.value.forEach((item:any)=> {
      if(chooseList.value.find(e=> e.id === item.id)) {
        item.selected = true
      }
    });
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

function toggle(record:any) {
  record.selected = !record.selected;
  if(record.selected) {
    chooseList.value.push(record)
  } else {
    chooseList.value.splice(chooseList.value.findIndex(e=>e.id===record.id), 1);
  }
}

function close() {
  visible.value = false;
  chooseList.value = [];
}

function submit() {
  emit("submit", chooseList.value);
  visible.value = false;
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {

})

defineExpose({
  open: (options:any = {})=> {
    visible.value = true
    chooseList.value = options.chooseList || [];

    queryParams.pageNo = 1;
    getList();
  }
})
</script>
